﻿using System;
using System.Xml.Linq;
using System.Xml.Serialization;
using PortalWeb.Common;

namespace PortalWeb.Flight.Core.Entity
{
    [Serializable]
    public class SegmentBase : IXmlObject
    {
        /// <summary>
        /// 航班航空公司二字码
        /// </summary>
        [XmlElement("aircode")]
        public string AirlineCode { get; set; }

        /// <summary>
        /// 离港机场代码
        /// </summary>
        [XmlElement("depcity")]
        public string DepCityCode { get; set; }

        /// <summary>
        /// 航班到达城市三字码
        /// </summary>
        [XmlElement("arrcity")]
        public string ArrCityCode { get; set; }

        /// <summary>
        /// 航班起飞时间
        /// </summary>
        [XmlElement("deptime")]
        public DateTime DepTime { get; set; }

        /// <summary>
        /// 航班到达时间
        /// </summary>
        [XmlElement("arrtime")]
        public DateTime ArrTime { get; set; }

        /// <summary>
        /// 航班号
        /// </summary>
        [XmlElement("flight")]
        public string FlightNo { get; set; }

        /// <summary>
        /// 舱位代码
        /// </summary>
        [XmlElement("cabin")]
        public string CabinCode { get; set; }

        /// <summary>
        /// 舱位代码
        /// </summary>
        [XmlElement("actioncode")]
        public string ActionCode { get; set; }

        /// <summary>
        /// 是否为共享航班
        /// </summary>
        [XmlElement("share")]
        public int Share { get; set; }

        public XElement ExportXml()
        {
            XElement xml = new XElement("segment");
            xml.SetElementValue("depcity", DepCityCode);
            xml.SetElementValue("arrcity", ArrCityCode);
            xml.SetElementValue("depdate", DepTime.ToString("yyyy-MM-dd"));
            xml.SetElementValue("deptime", DepTime.ToString("HH:mm"));
            xml.SetElementValue("arrtime", ArrTime.ToString("HH:mm"));
            xml.SetElementValue("flight", FlightNo);
            xml.SetElementValue("aircode", AirlineCode);
            xml.SetElementValue("cabin", CabinCode);
            xml.SetElementValue("actioncode", ActionCode);
            xml.SetElementValue("share", Share);
            return xml;
        }

        public void ImportXml(XElement xml)
        {
            DepCityCode = xml.GetElementValue("depcity", DepCityCode);
            ArrCityCode = xml.GetElementValue("arrcity", string.Empty);

            DateTime datretime;
            if (DateTime.TryParse(xml.GetElementValue("depdate", string.Empty) + " " +
                                  xml.GetElementValue("deptime", string.Empty), out datretime))
            {
                DepTime = datretime;
            }
            if (DateTime.TryParse(xml.GetElementValue("depdate", string.Empty) + " " +
                                  xml.GetElementValue("arrtime", string.Empty), out datretime))
            {
                ArrTime = datretime;
            }

            FlightNo = xml.GetElementValue("flight", FlightNo);
            AirlineCode = xml.GetElementValue("aircode", AirlineCode);

            //if (string.IsNullOrEmpty(AirlineCode))
            //{
            //    xElement = xml.Element("air");
            //    if (xElement != null)
            //        AirlineCode = xml.GetElementValue("air", AirlineCode);
            //}
            CabinCode = xml.GetElementValue("cabin", CabinCode);
            ActionCode = xml.GetElementValue("actioncode", ActionCode);
            Share = xml.GetElementValue("share", Share);
        }

        public void ImportXml(string xml)
        {
            try
            {
                ImportXml(XElement.Parse(xml));
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message + " xml：" + xml, ex);
            }
        }
    }
}
